package GUILayer;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import java.awt.Font;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.Color;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;

import ControlLayer.ProductCTR;
import ModelLayer.Product;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ProductGUI extends JFrame {

	private JPanel contentPane;
	private JTextField textProductID;
	private JTextField textName;
	private JTextField textQuantity;
	private JTextField textDescription;
	private JTextField textPrice;
	private JTextField textSalePrice;
	private JTextField textStock;
	private JTextField textRow;
	private JTextField textShelf;
	private JTextField txtInsertId;
	private JTextField txtEnterSerialno;
	private JTextField txtDelete;
	private ProductCTR proCTR;
	private JTextField txtFind;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProductGUI frame = new ProductGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ProductGUI()
	{
		proCTR = new ProductCTR();
		setTitle("Product");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		setSize(600, 400); 
		contentPane = new JPanel();
		contentPane.setBackground(Color.GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel PanelEnterInformations = new JPanel();
		PanelEnterInformations.setLayout(null);
		PanelEnterInformations.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "Enter Information", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 255, 255)));
		PanelEnterInformations.setBackground(Color.GRAY);
		PanelEnterInformations.setBounds(10, 32, 547, 213);
		contentPane.add(PanelEnterInformations);
		
		JButton btnAddProduct = new JButton("Add Product");
		btnAddProduct.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				createProduct();
			}
		});
		btnAddProduct.setToolTipText("Add a Product to the database");
		btnAddProduct.setBounds(13, 177, 150, 25);
		PanelEnterInformations.add(btnAddProduct);
		
		JButton btnFindProducts = new JButton("Find Products");
		btnFindProducts.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				
			}
		});
		btnFindProducts.setBounds(201, 177, 150, 25);
		PanelEnterInformations.add(btnFindProducts);
		
		JButton btnCurrentProducts = new JButton("Current Products");
		btnCurrentProducts.setBounds(385, 177, 150, 25);
		PanelEnterInformations.add(btnCurrentProducts);
		
		JLabel lblProductId = new JLabel("ID:");
		lblProductId.setHorizontalAlignment(SwingConstants.CENTER);
		lblProductId.setForeground(Color.WHITE);
		lblProductId.setBounds(10, 24, 100, 20);
		PanelEnterInformations.add(lblProductId);
		
		JLabel lblProductName = new JLabel("Name:");
		lblProductName.setHorizontalAlignment(SwingConstants.CENTER);
		lblProductName.setForeground(Color.WHITE);
		lblProductName.setBounds(145, 24, 100, 20);
		PanelEnterInformations.add(lblProductName);
		
		JLabel lblDescription = new JLabel("Description:");
		lblDescription.setHorizontalAlignment(SwingConstants.CENTER);
		lblDescription.setForeground(Color.WHITE);
		lblDescription.setBounds(290, 24, 100, 20);
		PanelEnterInformations.add(lblDescription);
		
		JLabel lblPrice = new JLabel("Cost Price:");
		lblPrice.setHorizontalAlignment(SwingConstants.CENTER);
		lblPrice.setForeground(Color.WHITE);
		lblPrice.setBounds(435, 24, 100, 20);
		PanelEnterInformations.add(lblPrice);
		
		JLabel lblPrice_1 = new JLabel("Sale Price:");
		lblPrice_1.setHorizontalAlignment(SwingConstants.CENTER);
		lblPrice_1.setForeground(Color.WHITE);
		lblPrice_1.setBounds(10, 74, 100, 20);
		PanelEnterInformations.add(lblPrice_1);
		
		JLabel lblSalePrice = new JLabel("Quantity:");
		lblSalePrice.setHorizontalAlignment(SwingConstants.CENTER);
		lblSalePrice.setForeground(Color.WHITE);
		lblSalePrice.setBounds(145, 74, 100, 20);
		PanelEnterInformations.add(lblSalePrice);
		
		textProductID = new JTextField();
		textProductID.setColumns(10);
		textProductID.setBounds(10, 47, 100, 20);
		PanelEnterInformations.add(textProductID);
		
		textName = new JTextField();
		textName.setColumns(10);
		textName.setBounds(145, 47, 100, 20);
		PanelEnterInformations.add(textName);
		
		textQuantity = new JTextField();
		textQuantity.setColumns(10);
		textQuantity.setBounds(145, 95, 100, 20);
		PanelEnterInformations.add(textQuantity);
		
		textDescription = new JTextField();
		textDescription.setColumns(10);
		textDescription.setBounds(290, 47, 100, 20);
		PanelEnterInformations.add(textDescription);
		
		textPrice = new JTextField();
		textPrice.setColumns(10);
		textPrice.setBounds(435, 47, 100, 20);
		PanelEnterInformations.add(textPrice);
		
		textSalePrice = new JTextField();
		textSalePrice.setColumns(10);
		textSalePrice.setBounds(13, 95, 100, 20);
		PanelEnterInformations.add(textSalePrice);
		
		textStock = new JTextField();
		textStock.setColumns(10);
		textStock.setBounds(290, 95, 100, 20);
		PanelEnterInformations.add(textStock);
		
		textRow = new JTextField();
		textRow.setColumns(10);
		textRow.setBounds(435, 95, 100, 20);
		PanelEnterInformations.add(textRow);
		
		textShelf = new JTextField();
		textShelf.setColumns(10);
		textShelf.setBounds(13, 143, 100, 20);
		PanelEnterInformations.add(textShelf);
		
		JLabel lblStock = new JLabel("Stock:");
		lblStock.setHorizontalAlignment(SwingConstants.CENTER);
		lblStock.setForeground(Color.WHITE);
		lblStock.setBounds(290, 74, 100, 20);
		PanelEnterInformations.add(lblStock);
		
		JLabel lblRow = new JLabel("Row:");
		lblRow.setHorizontalAlignment(SwingConstants.CENTER);
		lblRow.setForeground(Color.WHITE);
		lblRow.setBounds(435, 74, 100, 20);
		PanelEnterInformations.add(lblRow);
		
		JLabel lblShelf = new JLabel("Shelf:");
		lblShelf.setHorizontalAlignment(SwingConstants.CENTER);
		lblShelf.setForeground(Color.WHITE);
		lblShelf.setBounds(13, 122, 100, 20);
		PanelEnterInformations.add(lblShelf);
		
		JPanel PanelCreateSpecimen = new JPanel();
		PanelCreateSpecimen.setLayout(null);
		PanelCreateSpecimen.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "Create Specimen", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 255, 255)));
		PanelCreateSpecimen.setBackground(Color.GRAY);
		PanelCreateSpecimen.setBounds(10, 255, 170, 96);
		contentPane.add(PanelCreateSpecimen);
		
		JButton btnCreateSpecimen = new JButton("Create Specimen");
		btnCreateSpecimen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				createSpecimen();
			}
		});
		btnCreateSpecimen.setBounds(10, 61, 150, 25);
		PanelCreateSpecimen.add(btnCreateSpecimen);
		
		txtInsertId = new JTextField();
		txtInsertId.setText("Enter ID:");
		txtInsertId.setHorizontalAlignment(SwingConstants.LEFT);
		txtInsertId.setColumns(10);
		txtInsertId.setBounds(10, 30, 60, 20);
		PanelCreateSpecimen.add(txtInsertId);
		
		txtEnterSerialno = new JTextField();
		txtEnterSerialno.setText("Enter SerialNo:");
		txtEnterSerialno.setHorizontalAlignment(SwingConstants.LEFT);
		txtEnterSerialno.setColumns(10);
		txtEnterSerialno.setBounds(80, 30, 80, 20);
		PanelCreateSpecimen.add(txtEnterSerialno);
		
		JPanel PanelDeleteProduct = new JPanel();
		PanelDeleteProduct.setLayout(null);
		PanelDeleteProduct.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "Delete Product", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 255, 255)));
		PanelDeleteProduct.setBackground(Color.GRAY);
		PanelDeleteProduct.setBounds(387, 255, 170, 96);
		contentPane.add(PanelDeleteProduct);
		
		JButton btnDeleteProduct = new JButton("Delete Product");
		btnDeleteProduct.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				deleteProduct();
			}
		});
		btnDeleteProduct.setBounds(10, 60, 150, 25);
		PanelDeleteProduct.add(btnDeleteProduct);
		
		txtDelete = new JTextField();
		txtDelete.setText("Enter ID:");
		txtDelete.setHorizontalAlignment(SwingConstants.LEFT);
		txtDelete.setColumns(10);
		txtDelete.setBounds(10, 30, 150, 20);
		PanelDeleteProduct.add(txtDelete);
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "Find Product", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 255, 255)));
		panel.setBackground(Color.GRAY);
		panel.setBounds(201, 255, 170, 96);
		contentPane.add(panel);
		
		JButton btnFindProduct = new JButton("Find Product");
		btnFindProduct.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				findProduct();
			}
		});
		btnFindProduct.setBounds(10, 60, 150, 25);
		panel.add(btnFindProduct);
		
		txtFind = new JTextField();
		txtFind.setText("Enter ID:");
		txtFind.setHorizontalAlignment(SwingConstants.LEFT);
		txtFind.setColumns(10);
		txtFind.setBounds(10, 30, 150, 20);
		panel.add(txtFind);
	}
	private void createProduct()
	{
		try 
		{
			proCTR.createProduct(Integer.parseInt(textProductID.getText()), textName.getText(), textDescription.getText(), Double.valueOf(textPrice.getText()), Double.valueOf(textSalePrice.getText()),Integer.parseInt(textQuantity.getText()), Integer.parseInt(textStock.getText()), Integer.parseInt(textRow.getText()), Integer.parseInt(textShelf.getText()));
			System.out.println("Dette virker sku findt :) ");
		} 
		catch (Exception e) 
		{
			System.out.println("Der er sket en fejl ");
		}
		
	}
	private void findProduct()
	{
		try 
		{
		Product product = proCTR.findProduct(Integer.parseInt(txtFind.getText()));
		textProductID.setText(String.valueOf(product.getProductID()));
		textName.setText(product.getProductName());
		textDescription.setText(product.getdesc());
		textPrice.setText(String.valueOf(product.getPrice()));
		textSalePrice.setText(String.valueOf(product.getSalePrice()));
		textQuantity.setText(String.valueOf(product.getQuantity()));
		textStock.setText(String.valueOf(product.getStock()));
		textRow.setText(String.valueOf(product.getRow()));
		textShelf.setText(String.valueOf(product.getShelf()));
		System.out.println("Det virker sku");
		} 
		catch (Exception e) 
		{
			System.out.println("Det er sket en fejl");
		}
		
		
	}
	private void createSpecimen()
	{
		try 
		{
			proCTR.createSpeciman(Integer.parseInt(txtInsertId.getText()), Integer.parseInt(txtEnterSerialno.getText()));
			System.out.println("Dette virker sku findt :) ");
		} 
		catch (Exception e) 
		{

			System.out.println("Det er sket en fejl ");
		}
	}
	private void deleteProduct()
	{
		try 
		{
			proCTR.deleteProduct(Integer.parseInt(txtDelete.getText()));
			System.out.println("Dette virker sku findt :) ");
		} 
		catch (Exception e) 
		{
			System.out.println("Det er sket en fejl ");
		}
	}
	
	
}
